﻿Global.namespace("System.Web.Page");
System.Web.Page.InviteFriend=function(config){
    config.templateFile="InviteFriend.htm?t="+ Global.random();
    this.receiverId=config.receiverId;
    System.Web.Page.InviteFriend.superclass.constructor.call(this, config);
}
Global.extend(System.Web.Page.InviteFriend,System.Core.UI.BasePage,{
    onLoading:function(){
        Global.showLoadingMask({
            wrapper:'#'+this.id,
            id:'invite_Loading'
        });
    },
    onRendered:function(){
        Global.hideLoadingMask('invite_Loading');
        $('.FriendList').addClass('MinLoadingMask');
        var ctr=this;
        
        System.Web.Handler.RequestHandler.getFriends(function(response){
            var friends=response.get("friends").getData();
            ctr.bindFriends(friends);
            $('.FriendList').removeClass('MinLoadingMask');
        });
        
        $('#btnSend').click(function(){
            if(!ctr.validateInput()){ return;}
            ctr.submit();
        });
        
    },
    bindFriends:function(friends){
        var ctr=this;
        
        var repeater=new System.Core.UI.Repeater({
            isObjectDataStore:true,
            template:"<div id='{0}'></div>",
            wrapper:'.FriendList',
            dataStore:friends,
            itemTemplate:function(item){
                if(System.Web.Runtime.userDict &&
                    System.Web.Runtime.userDict.exists(item.getField(opensocial.Person.Field.ID))){ 
                        return "";
                    }
                var html='<li><img src="{0}" /><br /><input type="checkbox" {3} id="friend{1}" class="FriendItem" value="{1}" /><label for ="friend{1}">{2}</label></li>';
                return StringHandler.format(
                    html,
                    item.getField(opensocial.Person.Field.THUMBNAIL_URL),
                    item.getField(opensocial.Person.Field.ID),
                    item.getDisplayName(),
                    item.getField(opensocial.Person.Field.ID)===ctr.receiverId?' checked=true':''
                );
            },
            noItemTemplate:function(){
                return "<li>No frieds were found</li>"
            }
        });
        repeater.render();
        
        gadgets.window.adjustHeight();
        
    },
    validateInput:function(){
       
        if($.trim($('#txtContent').val())===''){
            $('#txtContent').addClass("ValidateError");
            return false;
        }else{
            $('#txtContent').removeClass("ValidateError");
        }
        return true;
    },
    submit:function(){
       
        var ctr=this;
        var content=$('#txtContent').val();
        var friends=$('.FriendItem:checked');
        var friendsId='';

        friends.each(function(index){
            friendsId+=friends[index].value+(index<friends.length-1?",":"");
        });
        
        content=StringHandler.format(
            System.Web.Lang.Message.InvitationMessage,
            "Thanh Tu",
            content
        );
        
        System.Web.Handler.MessageHandler.sendInvitation(
            friendsId,
            content,
            function(){
                // back to home page
                var masterPage=ctr.parent;
                masterPage.loadControl(System.Web.Page.Home, { wrapper: '.MasterPageWrapper',autoRender:true });
            }
        );
    }
});
